<?php
	require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
	include ABS_PATH . "header.php";	
	include ABS_PATH . "navigation.php";
	include ABS_PATH . "menu.php";

// navigation starts here
	global $cur_option;
	$cur_option = "Controller-Commands";
	menu($cur_option);
?>

<?php include ABS_PATH . "after-navigation.php"; ?>
            <!-- add your content here -->
            
<?php
	if (!isset($_REQUEST['PermURL'])) {
?>
			<form class="ym-form" action="controller-commands.php">
				<div class="ym-fbox">
					<div class="ym-fbox-wrap ym-grid">
						<div class="ym-g33 ym-gl">
							<div class="ym-gbox-left">
								<label for="PermURL">Master Bot Controller</label>
<?php
		// Get a list of controller devices and PermURLs
		
		include_once(ABS_PATH . "misc/functions.php");
		
		$db = $SQL = $result = null;
		
		try 
		{
			$db = new PDO(PDO_PREFIX . SQLITE_DB_FILENAME);
			
			$SQL = "SELECT Name, Location, Position, PermURL FROM Positions WHERE ObjectType ='Bot Controller' ORDER BY Name";
			$result = $db->query($SQL);
		}
			
		catch(PDOException $e)
		{
			die("Couldn't execute query $SQL: " . $e->getMessage());
		}
		
		try
		{	

?>
			<select name="PermURL" id="PermURL" size="1">
				<option value="0" selected="selected" disabled="disabled">Please choose a master bot controller</option>
<?php		
			while($row = $result->fetch(PDO::FETCH_ASSOC))
			{
				_log(print_r($row, TRUE));
				
				// parse name of the region and coordinates
                        
                $regionName = substr($row["Location"], 0, strpos($row["Location"], "(") - 1);
                $coords = trim($row["Position"], "() \t\n\r");
                $xyz = explode(",", $coords);
				
				printf('\t\t<option value="%s">%s [%s (%d,%d,%d)]</option>\n',
					$row['PermURL'], $row['Name'],
					$regionName, $xyz[0], $xyz[1], $xyz[2]);
			}
?>
			</select>
<?php
		}

		catch(PDOException $e)
		{
			_log(__FILE__ . " - line " . __LINE__ . ": Database connection error: " . PDO_PREFIX . SQLITE_DB_FILENAME . ": " . $e->getMessage());
			die("Empty database? " . $e->getMessage());		
			// output an input box instead
?>
								<input type="text" name="PermURL" id="PermURL" placeholder="Enter PermURL for bot controller" />
<?php			
			
		}
?>
							</div>
						</div> <!-- ym-g33 ym-gl -->						
						<div class="ym-g33 ym-gl">
							<div class="ym-gbox-left">
								<label for="NPC">Agent Name</label>
<?php
		// Get a list of avatar names and UUIDs
		
		include_once(ABS_PATH . "misc/functions.php");
		
		$db = $SQL = $result = null;
		
		try 
		{
			$db = new PDO(PDO_PREFIX . SQLITE_DB_FILENAME);
			
			$SQL = "SELECT Name, OwnerKey FROM Agents ORDER BY Name";
			$result = $db->query($SQL);
		}
			
		catch(PDOException $e)
		{
			die("Couldn't execute query $SQL: " . $e->getMessage());
		}
		
		try
		{	

?>
			<select name="NPC" id="NPC" size="1">
				<option value="0" selected="selected" disabled="disabled">Please choose an agent name</option>
<?php		
			while($row = $result->fetch(PDO::FETCH_ASSOC))
			{
				_log(print_r($row, TRUE));
				echo "\t\t<option value=\"" . $row['OwnerKey'] . "\">" . $row['Name'] . " (" . $row['OwnerKey']
					. ")</option>\n";
			}
?>
			</select>
<?php
		}

		catch(PDOException $e)
		{
			_log(__FILE__ . " - line " . __LINE__ . ": Database connection error: " . PDO_PREFIX . SQLITE_DB_FILENAME . ": " . $e->getMessage());
			die("Empty database? " . $e->getMessage());		
			// output an input box instead
?>
								<input type="text" name="NPC" id="NPC" placeholder="Enter UUID for agent" />
<?php			
			
		}
?>
							</div>
						</div> <!-- ym-g33 ym-gl -->
					</div> <!-- ym-fbox-wrap ym-grid -->
					<div class="ym-fbox-wrap ym-grid">
						<div class="ym-g33 ym-gl">
							<div class="ym-gbox-left">
								<label for="command">Commands</label>
								<select name="command" id="command" size="1">
								    <option value="0" selected="selected" disabled="disabled">Please choose</option>
								    <optgroup label="Creation and Destruction">
								    	<option value="osNpcCreate">osNpcCreate</option>
										<option value="osNpcRemove">osNpcRemove</option>
								    </optgroup>
								    <optgroup label="Get and Set">
								    	<option value="osNpcGetRot">osNpcGetRot</option>
										<option value="osNpcSetRot">osNpcSetRot</option>
										<option value="osNpcGetPos">osNpcGetPos</option>
										<option value="osNpcGetOwner">osNpcGetOwner</option>
								    </optgroup>
								    <optgroup label="Movement">
								    	<option value="osNpcMoveToTarget">osNpcMoveToTarget</option>
										<option value="osNpcStopMoveToTarget">osNpcStopMoveToTarget</option>
								    </optgroup>
								    <optgroup label="Sitting and Standing">
								    	<option value="osNpcSit">osNpcSit</option>
										<option value="osNpcStand">osNpcStand</option>
								    </optgroup>
								    <optgroup label="Communication">
								    	<option value="osNpcSay">osNpcSay</option>
										<option value="osNpcShout">osNpcShout</option>
										<option value="osNpcWhisper">osNpcWhisper</option>
									</optgroup>
								    <optgroup label="Animations">
								    	<option value="osNpcPlayAnimation">osNpcPlayAnimation</option>
										<option value="osNpcStopAnimation">osNpcStopAnimation</option>
								    </optgroup>
								    <optgroup label="Appearance">
								    	<option value="osNpcLoadAppearance">osNpcLoadAppearance</option>
								    </optgroup>					    
								    <optgroup label="Touch">
								    	<option value="osNpcTouch">osNpcTouch</option>
								    </optgroup>
								    <optgroup label="Engine Get Values">
								    	<option value="getMoney" disabled="disabled">getMoney</option>
								    	<option value="getHappiness" disabled="disabled">getHappiness</option>
								    	<option value="getEnergy" disabled="disabled">getEnergy</option>
								    	<option value="getHome" disabled="disabled">getHome</option>
								    	<option value="getClass" disabled="disabled">getClass</option>
								    	<option value="getSubType" disabled="disabled">getSubType</option>
								    </optgroup>								    
								    <optgroup label="Engine Set Values">
								    	<option value="setMoney" disabled="disabled">setMoney</option>
								    	<option value="setHappiness" disabled="disabled">setHappiness</option>
								    	<option value="setEnergy" disabled="disabled">setEnergy</option>
								    	<option value="setHome" disabled="disabled">setHome</option>
								    	<option value="setClass" disabled="disabled">setClass</option>
								    	<option value="setSubType" disabled="disabled">setSubType</option>
								    </optgroup>	
								</select>
							</div> <!-- ym-gbox-right -->
						</div> <!-- ym-g33 ym-gl -->
					</div> <!-- ym-fbox-wrap ym-grid -->
					<div class="ym-fbox-wrap ym-grid">
						<div class="ym-g33 ym-gl">
							<div class="ym-gbox-left">
								<label for="param1">Parameter #1 Type</label>
								<select name="param1" id="param1" size="1">
									<option value="string" selected="selected">string</option>
									<option value="key">key</option>
									<option value="int">int</option>
									<option value="float">float</option>
									<option value="vector">vector</option>
								</select>
							</div>
						</div>
						<div class="ym-g33 ym-gl">
							<div class="ym-gbox-right">
								<label for="data1">Parameter #1 Data</label>
								<input type="text" name="data1" id="data1" placeholder="Enter data" />
							</div>
						</div>
					</div>
					<div class="ym-fbox-wrap ym-grid">
						<div class="ym-g33 ym-gl">
							<div class="ym-gbox-left">
								<label for="param2">Parameter #2 Type</label>
								<select name="param2" id="param2" size="1">
									<option value="string" selected="selected">string</option>
									<option value="key">key</option>
									<option value="int">int</option>
									<option value="float">float</option>
									<option value="vector">vector</option>

								</select>
							</div>
						</div>
						<div class="ym-g33 ym-gl">
							<div class="ym-gbox-right">
								<label for="data2">Parameter #2 Data</label>
								<input type="text" name="data2" id="data2" placeholder="Enter data" />
							</div>
						</div>
					</div> <!-- ym-fbox-wrap ym-grid -->
				</div> <!-- ym-fbox -->
				<div class="ym-fbox-footer">
					<div class="ym-fbox-wrap ym-grid">
						<div class="ym-g20 ym-gl">
							<div class="ym-gbox-left">
								<input type="submit" class="ym-button ym-primary" value="Submit" id="submit"/>
							</div>
						</div>
						<div class="ym-g20 ym-gl">
							<div class="ym-gbox-right">
								<input type="reset" class="ym-button ym-warning" value="Reset" id="reset"/>
							</div>
						</div>
					</div>
				</div>
			</form>
<?php
	}
	else {		
		var_dump($_REQUEST);

		// connect to remote agent via cURL and see if we can get the request through

        // prepare to pass parameters as a post
        $encodedRequest = rawurlencode(
        	"npc=" . $_REQUEST["NPC"] . "&" .
        	"command=" . $_REQUEST["command"] . "&" . 
        	$_REQUEST["param1"] . "=" . $_REQUEST["data1"] . "&" .
        	$_REQUEST["param2"] . "=" . $_REQUEST["data2"]
		);
		echo "<br />Trying to contact " . $_REQUEST["PermURL"] . " with " . $encodedRequest . " ...<br />";

		$output = "";
		
		try {
			// create curl resource 
	        $ch = curl_init(); 
	
	        // set url 
	        curl_setopt($ch, CURLOPT_URL, $_REQUEST["PermURL"]);
	        curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedRequest);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_POST, 1);
	
	        //return the transfer as a string 
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

        	// $output contains the output string 
			$output = curl_exec($ch);
			
			// close curl resource to free up system resources 
			curl_close($ch); 
		}
		catch (Exception $e) {
			echo $e->getCode() . ": " . $e->getMessage() . "<br />";
			echo "cURL error: " . curl_error($ch) . "<br />";
			die;
		}

		echo "cURL call returned: -->" . $output . " <--<br />";
?>
	<div>
		<a href="controller-commands.php" class="ym-button ym-next">Another command</a>
	</div>
<?php
	}
?>
<?php include ABS_PATH . "footer.php"; ?>